Page({
  data: {
    adminInfo: null,
    pendingMerchants: 0,
    abnormalOrders: 0,
  },

  onLoad() {
    this.checkAdminLogin();
    this.loadAdminInfo();
    this.loadPendingCount();
  },

  onShow() {
    this.checkAdminLogin();
    this.loadPendingCount();
  },

  checkAdminLogin() {
    const adminToken = wx.getStorageSync("adminToken");
    if (!adminToken) {
      wx.redirectTo({
        url: "/pages/login/login",
      });
    }
  },

  loadAdminInfo() {
    const adminInfo = wx.getStorageSync("adminInfo");
    this.setData({ adminInfo });
  },

  loadPendingCount() {
    // TODO: 从后端获取待处理数据
    this.setData({
      pendingMerchants: 3, // 示例：3个待审核商家
      abnormalOrders: 2, // 示例：2个异常订单
    });
  },

  // 商家管理相关
  navigateToMerchantAudit() {
    wx.navigateTo({
      url: "/pages/admin/merchant-audit/merchant-audit",
    });
  },

  navigateToMerchantManage() {
    wx.navigateTo({
      url: "/pages/admin/merchant-manage/merchant-manage",
    });
  },

  // 用户管理相关
  navigateToUserManage() {
    wx.navigateTo({
      url: "/pages/admin/user-manage/user-manage",
    });
  },

  navigateToAdminManage() {
    wx.navigateTo({
      url: "/pages/admin/admin-manage/admin-manage",
    });
  },

  // 订单管理
  navigateToOrderManage() {
    wx.navigateTo({
      url: "/pages/admin/order-manage/order-manage",
    });
  },

  // 数据统计相关
  navigateToDataAnalysis() {
    wx.navigateTo({
      url: "/pages/admin/data-analysis/data-analysis",
    });
  },

  navigateToAIModel() {
    wx.navigateTo({
      url: "/pages/admin/ai-model/ai-model",
    });
  },

  handleLogout() {
    wx.showModal({
      title: "提示",
      content: "确定要退出登录吗？",
      success: (res) => {
        if (res.confirm) {
          // 清除管理员登录信息
          wx.removeStorageSync("adminToken");
          wx.removeStorageSync("adminInfo");

          wx.redirectTo({
            url: "/pages/login/login",
          });
        }
      },
    });
  },
});
